//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short is_active = 0;
short ae_count = 0;
short last_abil2;
short last_abil;

body;

beginstate INIT_STATE;
	set_name(ME,"Flailing Mass");
	change_max_health(ME,300);
	set_level(ME,38);
	set_boss_level(ME,2);
	
	set_resistance(ME,0,100);
	set_resistance(ME,1,100);
	set_resistance(ME,2,100);
	set_resistance(ME,3,100);
	set_resistance(ME,4,100);
	set_resistance(ME,5,100);
	set_resistance(ME,6,100);
	
	set_resistance(ME,7,100);

	last_abil = get_current_tick();
	last_abil2 = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(80,24,1);
	sf(80,9,1);
	sf(80,10,0);
	begin_talk_mode(10);
break;

beginstate START_STATE; 
	if (is_active == 0) {
		if (party_near_nav(1,6)) {
			is_active = 1;
			set_attitude(ME,10);
			
			sf(80,10,1);
			sf(80,24,0);

			erase_char(34);
			erase_char(35);
			
			set_resistance(ME,0,50);
			set_resistance(ME,1,30);
			set_resistance(ME,2,30);
			set_resistance(ME,3,0);
			set_resistance(ME,4,10);
			set_resistance(ME,5,10);
			set_resistance(ME,6,30);
	
			begin_talk_mode(9);
			}
		set_attitude(ME,4);
		end();
		}
		
	if (who_shot_me() >= 0) {


		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}

	if ((is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		last_abil = get_current_tick();
		ae_count = ae_count + 1;
		run_char_animation(2,1,35);	
		  	place_particle_num(ME,2,4,8);
		
		
		
		if (ae_count < 4) {
			if (ae_count < 3)
				print_named_str(ME,"is growing warmer and warmer.");
				else print_named_str(ME,"is almost fully charged.");
			end();
			}
		ae_count = 0;
			print_named_str(ME,"surrounds itself with a curtain of flame.");
			pc_heard_sound_delay(117,250);						
	  		create_missile_spiral(154,40,8,2);
	  		damage_nearby(get_ran(1,700,800),8,2,0);


		end();
		}

	if ((is_combat()) && 
	  (tick_difference(last_abil2,get_current_tick()) > 1)) {
		last_abil2 = get_current_tick();
		if (char_ok(39) == FALSE) {
			if (summon_creature(31)) {
				set_summon_level(39,1);
				place_particle_num(39,10,7,10);
				set_attack_bonus(39,10);
				
				print_named_str(ME,"makes a new creation!");
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				end();
				}
			}		
		else if (char_ok(40) == FALSE) {
			if (summon_creature(32)) {
				set_summon_level(40,1);
				place_particle_num(40,10,7,10);
				set_attack_bonus(40,10);
				
				print_named_str(ME,"makes a new creation!");
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				end();
				}
			}		
		}
		
	do_attack();

break;

beginstate TALKING_STATE;
	print_str("All it really is is a big fungus. It can't communicate at all.");
break;